package github.kawaiior.yuanchuxiuzhen.common.item.weapon.refine;

import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.util.EntityUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.ActionResult;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Hand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;

import java.util.List;

/**
 * 五行剑
 * 拥有不同的灵根能有不同的效果
 * 如果拥有多个灵根，则可以同时触发多种效果
 * 金: 攻击生物时，对周围3格IMOB生物造成50%溅射伤害
 * 木: 攻击生物时，获得5秒的生命恢复
 * 水: 攻击生物时，生物获得5秒的虚弱buff
 * 火: 右键生物时，获得5秒的力量buff
 * 土: 右键生物时，瞬移到生物附近
 */
public class FiveElementsSword extends RefineWeaponBase{

    @Override
    public float getAttackDamage() {
        return 49F;
    }

    @Override
    public boolean onLeftClickEntity(ItemStack stack, PlayerEntity player, Entity entity) {
        if (!(entity instanceof LivingEntity)){
            return false;
        }
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            if (power.hasManaRoot(ManaPower.ManaRoot.METAL)){
                AxisAlignedBB boundingBox = entity.getBoundingBox().grow(3.0);
                List<Entity> entities = player.world.getEntitiesWithinAABB(Entity.class, boundingBox);
                for (Entity e : entities) {
                    if (e instanceof IMob && !e.equals(entity)) {
                        e.attackEntityFrom(DamageSource.causeIndirectMagicDamage(player, player), (getAttackDamage()+power.getAttack())/2);
                    }
                }
            }
            if (power.hasManaRoot(ManaPower.ManaRoot.WOOD)){
                player.addPotionEffect(new EffectInstance(Effects.REGENERATION, 100, 1));
            }
            if (power.hasManaRoot(ManaPower.ManaRoot.WATER)){
                ((LivingEntity)entity).addPotionEffect(new EffectInstance(Effects.WEAKNESS, 100, 1));
            }
        });
        return false;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (worldIn.isRemote){
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }

        LazyOptional<ManaPower> capability = playerIn.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            if (power.hasManaRoot(ManaPower.ManaRoot.FIRE)){
                playerIn.addPotionEffect(new EffectInstance(Effects.STRENGTH, 100, 1));
            }
            if (power.hasManaRoot(ManaPower.ManaRoot.EARTH)){
                Entity target = EntityUtil.getPlayerLookTarget(worldIn, playerIn, 128D);
                if (target instanceof LivingEntity) {
                    double dx = target.getPosX() - playerIn.getPosX();
                    double dy = target.getPosY() - playerIn.getPosY();
                    double dz = target.getPosZ() - playerIn.getPosZ();

                    double distance = MathHelper.sqrt(dx * dx + dy * dy + dz * dz);

                    double d = 3;

                    double offsetX = d * (dx / distance);
                    double offsetY = d * (dy / distance);
                    double offsetZ = d * (dz / distance);

                    playerIn.setPositionAndUpdate(target.getPosX() - offsetX, target.getPosY() - offsetY, target.getPosZ() - offsetZ);
                }
            }
        });
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    public static Entity glowingEntity;

    @Override
    public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
        if (worldIn.isRemote){
            if (isSelected && entityIn instanceof PlayerEntity){
                Entity target = EntityUtil.getPlayerLookTarget(worldIn, (PlayerEntity)entityIn, 128D);
                if (target==null){
                    if (glowingEntity==null) {
                        return;
                    }
                    if (glowingEntity.isAlive()){
                        glowingEntity.setGlowing(false);
                    }
                    glowingEntity = null;
                    return;
                }

                if (target.equals(glowingEntity)){
                    return;
                }

                if (glowingEntity==null){
                    glowingEntity = target;
                    target.setGlowing(true);
                    return;
                }

                if (glowingEntity.isAlive()){
                    glowingEntity.setGlowing(false);
                }
                target.setGlowing(true);
                glowingEntity = target;
            }
        }
    }
}
